package jnpf.task;

import jnpf.entity.ZXGL_XiangMuFenJie_mingxibiaoEntity;
import jnpf.entity.ZXGL_XiangMuLiXiang_MingXiEntity;
import jnpf.service.ZXGL_XiangMuFenJie_mingxibiaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class ZXGL_XiangMuFenJie_mingxibiaoTask {

    @Autowired
    private ZXGL_XiangMuFenJie_mingxibiaoService zxglXiangMuFenJieMingxibiaoService;

    /**
     * 每天6点更改当天推送数据推送状态
     */
    // @Scheduled(cron = "0 0 2 * * ?")
    @Scheduled(fixedDelay = 1 * 60 * 1000)   //每隔1分钟执行
    public void execute() {

        List<ZXGL_XiangMuFenJie_mingxibiaoEntity> list = zxglXiangMuFenJieMingxibiaoService.getTodayList();
        for (ZXGL_XiangMuFenJie_mingxibiaoEntity entity : list) {
            entity.setZt("667276298107753797");
            entity.setSjksrq(entity.getJhksrq());
        }
        zxglXiangMuFenJieMingxibiaoService.updateBatchById(list);


        // 定时任务判断，根据项目计划开始日期、任务计划开始日期判断，状态自动变更为进行中，
        //同时将计划开始日期赋值给实际开始日期；
        //在计划完成日期前5天将未完成项目或任务状态变更为预警，超过计划完成日期后变更为超期，超过计划完成日期后5天变更为严重超期

        list = zxglXiangMuFenJieMingxibiaoService.getUnfinishedBefore5Day();
        Date now = new Date();
        for (ZXGL_XiangMuFenJie_mingxibiaoEntity entity : list) {
            // 计算时间差（毫秒）
            long diffInMillis = entity.getJhwcrq().getTime() - now.getTime();

            // 转换为其他单位
            long diffInSeconds = diffInMillis / 1000;
            long diffInMinutes = diffInSeconds / 60;
            long diffInHours = diffInMinutes / 60;
            long diffInDays = diffInHours / 24;

            if (diffInDays > 0) {
                // 提前5天标记预警
                entity.setYjbj("667376667227854149");
            } else if (diffInDays <= 0 && diffInDays >= -5) {
                // 超期五天之内
                entity.setYjbj("667376765865301317");
            } else if (diffInDays < -5) {
                // 超期五天之外
                entity.setYjbj("667376817690121541");
            }
        }

        zxglXiangMuFenJieMingxibiaoService.updateBatchById(list);


        List<String> yjbjs = new ArrayList<>();
        yjbjs.add("667376667227854149");
        yjbjs.add("667376765865301317");
        yjbjs.add("667376817690121541");
        List<ZXGL_XiangMuFenJie_mingxibiaoEntity> overdueList = zxglXiangMuFenJieMingxibiaoService.getOverdueList(yjbjs);
        if (overdueList != null && overdueList.size() > 0) {
            for (ZXGL_XiangMuFenJie_mingxibiaoEntity entity : overdueList) {
                // 计算时间差（毫秒）
                long diffInMillis = entity.getJhwcrq().getTime() - now.getTime();

                // 转换为其他单位
                long diffInSeconds = diffInMillis / 1000;
                long diffInMinutes = diffInSeconds / 60;
                long diffInHours = diffInMinutes / 60;
                long diffInDays = diffInHours / 24;

                // 超出五天
                if (diffInDays > 5) {
                    entity.setYjbj("");
                } else if (diffInDays > 0) {
                    // 提前5天标记预警
                    entity.setYjbj("667376667227854149");
                } else if (diffInDays <= 0 && diffInHours >= -5) {
                    // 超期五天之内
                    entity.setYjbj("667376765865301317");
                } else if (diffInDays < -5) {
                    // 超期五天之外
                    entity.setYjbj("667376817690121541");
                }
            }
        }
        zxglXiangMuFenJieMingxibiaoService.updateBatchById(overdueList);
    }
}
